$(function () {
    var form = window.form_query;
    var now = moment().format("YYYY-MM-DD");
    var timeLimitStart = form.timeLimitStart.value;
    var timeLimitEnd = form.timeLimitEnd.value;


    var startDate = moment(now).subtract(7, 'days').startOf('day'), endDate = moment(now).endOf('day');
    if (timeLimitStart && timeLimitEnd) {
        startDate = moment(parseInt(timeLimitStart));
        endDate = moment(parseInt(timeLimitEnd));
    }

    $("#timelimitstart_and_timelimitstart").daterangepicker({
        "startDate": startDate,
        "endDate": endDate,
        "opens": "center",
        "autoApply": true,
        "ranges": {
            "今天": [moment(now).startOf('day'), moment(now).endOf('day')],
            "昨天": [moment(now).subtract(1, 'days').startOf('day'), moment(now).subtract(1, 'days').endOf('day')],
            "最近7天": [moment(now).subtract(7, 'days').startOf('day'), moment(now).endOf('day')],
            "最近30天": [moment(now).subtract(30, 'days').startOf('day'), moment(now).endOf('day')],
            "本月": [moment(now).startOf('month'), moment(now).endOf('month')],
            "上个月": [moment(now).subtract(1, 'months').startOf('month'), moment(now).subtract(1, 'months').endOf('month')]
        },
        "locale": {
            "firstDay": 1,
            "format": "YYYY-MM-DD",
            "separator": " 至 ",
            "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
            "monthNames": ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
            customRangeLabel: "自定义范围"
        }
    }, function (start, end, label) {
        form.timeLimitStart.value = start.startOf('day').format("x");
        form.timeLimitEnd.value = end.endOf('day').format("x");
    });
});


//数据导出
$("[name='btn_export']").click(function () {
    var keyword = $("[name='keyword']").val();
    var salesmanId = $("[name='salesmanId']").val();
    var timeLimitStart = $("[name='timeLimitStart']").val();
    var timeLimitEnd = $("[name='timeLimitEnd']").val();
   
    //有数据时才能导出
    if($("#resultTotal_hidden").val() != 0){
    	bootbox.bconfirm("确定是否导出？", function (re) {
            if (re) {
            	 var url="/salesman/salehistory/export?keyword="+keyword+"&salesmanId="+salesmanId
            	 +"&timeLimitStart="+timeLimitStart+"&timeLimitEnd="+timeLimitEnd;
                 window.open(url);
            }
        });
    }else{
    	bootbox.balert("没有数据，无法导出!");
    }
});